'use strict';

const Service = require('egg').Service;

class kanbanService extends Service {

  /*
  * 查询list 
  * Method: get
  * path: /xxxx
  */

  async index(query, fields, options) {
    const results = await this.ctx.model.Kanban.find(query, fields, options)
    return results
  }

  /*
  * collection信息
  * obj: 查询的key 和 value  {id: id}
  * 
  */
  async show(query) {
    const results = await this.ctx.model.Kanban.findOne(query)
                          .populate({path: 'user'})
                          .populate({path: 'moduleList', populate: {path: 'module'}}).lean()
    return results
  }

  /*
  * 创建
  * Method: post
  * path: /xxxx
  */
  async create(data) {
    const result = await this.ctx.model.Kanban.create(data)
    return result
  }

  /*
  * 修改更新
  * Method: put
  * path: /xxxx/:id
  */
  async update(query, data) {
    const result = await this.ctx.model.Kanban.update(query, data)
    return result
  }

  /*
  * 删除
  * Method: DELETE
  * obj: {key: keyValue}
  */
  async destroy(query) {
    const result = await this.ctx.model.Kanban.remove(query)
    return result
  }

  // 看板添加展示模块
  async addModule(kanbanId, data) {
    const document = await this.ctx.model.Kanban.findById(kanbanId)
    document.moduleList.push(...data)
    return await document.save()
  }

  // 看板删除展示模块
  async removeModule(kanbanId, moduleId) {
    const document = await this.ctx.model.Kanban.findById(kanbanId)
    document.moduleList.id(moduleId).remove()
    return await document.save()
  }
  
}

module.exports = kanbanService
